<?php

namespace App\Http\Controllers\Home;

use App\Models\News;
use App\Models\Praises;
use App\Models\Seos;
use App\Models\Users;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ColumnController extends Controller
{
    protected $chanel_id = 5;//设置导航选中标志

    public function __construct()
    {
        view()->share('chanel_id',$this->chanel_id);
        view()->share('seo',Seos::seo('SEO-COLUMN'));
    }

    public function index(Request $request)
    {
        $fields = ['avatar','id','truename','introduce'];
        $params = [
            'title'  => (string)$request->get('title',''),
        ];
        $lists = Users::list($fields,$params,12);
        if($request->ajax()){
            return $this->writeJson(0,$lists);
        }
        return view('home.column.index');
    }

    public function info(Request $request)
    {
        $id = (int)$request->get('id',0);
        if(empty($id)){
            return $this->writeJson(1,null,'参数错误');
        }
        //获取文章列表
        $params = [
            'author_id' => $id,
            'status'    => News::STATUS_1,
        ];
        $fields = ['id','title','summary','view','created_at','thumb','cateid','author_id','status','catepid','praise'];
        $lists = News::home($fields,$params,10);
        if($request->ajax()){
            return $this->writeJson(0,$lists);
        }

        //详情
        $info = Users::select(['avatar','truename','introduce','column'])
//            ->where('id','>',1)
            ->where('column',Users::COLUMN_2)
            ->findOrFail($id);

        //统计文章数
        $total_news = News::countNews($id);
        //统计浏览量
        $total_views = News::countViews($id);
        $info['total_news'] = $total_news;
        $info['total_views'] = $total_views;

        //热门新闻
        $news_hot = News::newHot();
        $lists = json_encode($lists);
        return view('home.column.info',compact('info','lists','id','news_hot'));
    }

}